package com.des1gn.ymgetil.util;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * @Author Des1gn
 * @param <T>
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ResultModel<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	private static final String SUCCESS_MSG = "调用成功";
	public static final int CODE_SUCCESS = 0;
	public static final int CODE_FAILURE = 1;
	public static final String RESULT_SUCCESS = "success";
	public static final String RESULT_FAIL = "fail";

	private int code = CODE_SUCCESS;
	private T data;
	private String message;
	private String result = RESULT_SUCCESS;

	public static <T extends Object> ResultModel<T> createSuccess() {
		ResultModel<T> result = new ResultModel<>();
		result.message = SUCCESS_MSG;
		return result;
	}

	@SuppressWarnings("rawtypes")
	public static <T extends Object> ResultModel<T> createSuccess(T data) {
		ResultModel<T> result = ResultModel.createSuccess();
		result.setData(data);
		return result;
	}

	public static <T extends Object> ResultModel<T> createSuccess(String mssage,T data) {
		ResultModel<T> result = ResultModel.createSuccess();
		result.setMessage(mssage);
		result.setData(data);
		return result;
	}

	public static <T extends Object> ResultModel<T> createFail(String message) {
		return createFail(1, message);
	}

	public static <T extends Object> ResultModel<T> createFail(int code, String message) {
		return createFail(code,message,null);
	}
	public static <T extends Object> ResultModel<T> createFail(int code, String message, T data) {
		ResultModel<T> result =  new ResultModel<>();
		result.setMessage(message);
		result.setResult(RESULT_FAIL);
		result.setCode(code);
		result.setData(data);
		return result;
	}

}
